package com.taplinker.client.im.activity.message;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;
import com.taplinker.client.AppConfigContext;
import com.taplinker.client.R;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.client.im.util.SmileyParser;
import com.taplinker.core.util.ImageUtil;
import com.taplinker.core.util.TimeUtil;
import com.ti.winona.util.RegexUtil;

public class RightTextMessage extends AbstractMessageView{

	@Override
	public View getMessageView(final Context context, final TapLinkerMessage message, View convertView) {
		TextMessageHolder textMessageHolder = null;
		if(convertView == null) {
			convertView = LayoutInflater.from(context).inflate(MessageTypeEnum.getMessageType(message).getLayoutId(), null);
			textMessageHolder = new TextMessageHolder();
			textMessageHolder.name = (TextView) convertView.findViewById(R.id.name_text_tv);
			textMessageHolder.head = (SmartImageView) convertView.findViewById(R.id.head_icon_iv);
			textMessageHolder.content = (TextView) convertView.findViewById(R.id.content_text_tv);
			textMessageHolder.time = (TextView) convertView.findViewById(R.id.time_text_tv);
			textMessageHolder.state = (TextView) convertView.findViewById(R.id.sender_state_tv);
			convertView.setTag(textMessageHolder);
		} else {
			textMessageHolder = (TextMessageHolder) convertView.getTag();
		}
//		if(!TextUtils.isEmpty(message.getIcon())) {
			String headUrl = AppConfigContext.WEB_DISK_ROOT + message.getIcon();
//			ImageManager.getInstance().loadImage(headUrl, textMessageHolder.head, 45, 45,null);
			// 取出自己的头像
//			SharedPreferences sp = context.getSharedPreferences(Base1Activity.SP_FILE_NAME, Context.MODE_APPEND);
//			headUrl = sp.getString(Base1Activity.USER_IMAGE_URL, null);
//			
//			textMessageHolder.name.setText(sp.getString(Base1Activity.USER_NICK_NAME, ""));
			
			if( headUrl!=null && !"".equals(headUrl.trim()) ) {
				textMessageHolder.head.setImageUrl(headUrl);
			}
//			Log.v("log_ktian", "我的头像TEXT:"+headUrl);
//		}
		String content = message.getContent();
		if(!TextUtils.isEmpty(content)) {
			if(content.contains("&nbsp") || content.contains("nbsp")) {
				content = content.replaceAll("?nbsp", "");
			}
		}
		String textContent = content;
		String url = RegexUtil.parseUrl(message.getContent()).trim();
		textContent = textContent.replaceAll(url, "") ;
		
		SmileyParser parser = new SmileyParser(context);
		textMessageHolder.content.setText(parser.replace(textContent));
		// 设置颜色
		if(url!=null && !"".equals(url)) {
			textMessageHolder.content.setTextColor(Color.parseColor("#fc0000"));
		}else{
			textMessageHolder.content.setTextColor(Color.parseColor("#000000"));
		}
		
		
		textMessageHolder.content.setOnLongClickListener(new MessageOnLongClickListener(message, context));
		textMessageHolder.state.setText("");
		textMessageHolder.state.setPadding(
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3),
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3));
		textMessageHolder.state.setBackgroundResource(R.drawable.message_state_bg);
		if (message.isUnarrived()){
			// 切换未送到消息图标提示
			textMessageHolder.state.setTextColor(Color.RED);
			textMessageHolder.state.setPadding(0, 0, 0, 0);
			textMessageHolder.state.setBackgroundResource(R.drawable.msg_warning);
		} else if (message.getSendTime() == null) {
			//textMessageHolder.state.setText("发送中");
		//	textMessageHolder.state.setTextColor(0xff4dae4b);
		} else if (message.getRead()) {
		//	textMessageHolder.state.setText("已读");
			textMessageHolder.state.setBackgroundResource(R.drawable.input_read);
			//textMessageHolder.state.setTextColor(0xff4dae4b);
		} else {
//			textMessageHolder.state.setText("已送达");
//			textMessageHolder.state.setTextColor(0xffff8a00);
			textMessageHolder.state.setBackgroundResource(R.drawable.input_songda);
		}
		if(message.getSendTime() != null) {
			textMessageHolder.time.setText(TimeUtil.timeFormat(message.getSendTime()));
		}
		textMessageHolder.content.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = "";
				// 过滤出http连接
				url = RegexUtil.parseUrl(message.getContent());
				// 点击打开网页地址
				if(url!=null && !"".equals(url)) {
					Intent intent = new Intent();        
			        intent.setAction("android.intent.action.VIEW");
			        Uri content_url = Uri.parse(url);   
			        intent.setData(content_url);  
			        context.startActivity(intent);
				}
			}
		});
		return convertView;
	}
	
	class TextMessageHolder {
		TextView name;		// 用户名
		SmartImageView head;		// 头像
		TextView content;	// 内容
		TextView time;		// 时间
		TextView state;		// 发送状态
	}

}